package com.yzdmw.toutiao.user;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavDirections;
import androidx.navigation.Navigation;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.yzdmw.toutiao.R;
import com.yzdmw.toutiao.login.LoginActivity;
import com.yzdmw.toutiao.wheeldisk.ui.WheeldiskFragmentDirections;

public class UserUnsignedFragment extends Fragment {

    private UserViewModel mViewModel;
    private Button login;

    public static UserUnsignedFragment newInstance() {
        return new UserUnsignedFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.user_unsigned_fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(requireActivity()).get(UserViewModel.class);
        login = this.getView().findViewById(R.id.user_login);
        login.setOnClickListener(view -> {
            navigateToLogin();
        });
    }

    private void navigateToLogin() {
        Intent intend = new Intent(requireActivity(), LoginActivity.class);
        startActivityForResult(intend,11);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("USER","requestCode:"+requestCode+",resultCode:"+resultCode);
        if (requestCode == 11 && resultCode == 22) {
            Log.d("loginResult","ok");
            boolean result = data.getBooleanExtra("login",false);
            if (result) mViewModel.setIsSigned(true);
        }
    }
}